 /*
 * 视频专区管理视图
 * @Author: LuoQianJun
 * @Date: 2018-01-25 15:36:18
 * @Last Modified by: ljb
 * @Last Modified time: 2018-01-30 15:06:58
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-video-views {
        padding: 20px;
    }

</style>

 <template>

    <div class="v-admin-video-views">

        <!-- 面包屑组件 -->
        <vBreadcrumb :breadcrumbList="breadcrumbList"></vBreadcrumb>

        <Tabs type="card" @on-click="changeTab">

            <TabPane label="新增" name="新增">
                
                <video-create   ref="create"
                                :bos-config="bos_config"
                                :video-types="video_types"
                                @post="postData">
                </video-create>

            </TabPane>

            <TabPane label="查看" name="查看">

                <!-- 搜索组件 -->
                <search-form  @search="confirmSearch" :video-types="video_types"></search-form>
                
                <video-index    :loading="table_loading"
                                :video-types="video_types"
                                :videos="videos.data"
                                @edit="videoDetail"
                                @delete="deletevideo"
                                @sort="confirmSearch">                       
                </video-index>

                <br>

                <!-- 分页组件 -->
                <Row type="flex" justify="center">
                    <Page @on-change="pageChange" 
                        @on-page-size-change="perPageChange"
                        placement="top"
                        :total="videos.total" 
                        :current="videos.current_page"
                        :page-size="query_info.per_page"
                        show-sizer show-total></Page>
                </Row>

            </TabPane>
        </Tabs>

        <Modal title="修改视频"
                v-model="show_modal">

            <!-- 视频编辑组件 -->
            <video-edit :video-types="video_types"
                        :video-detail="video_detail"
                        @update="updatevideo"></video-edit>

            <div slot="footer"></div>

        </Modal>

    </div>

 </template>
 
 <script>

import API_LIST from 'src/API_LIST';

import vBreadcrumb from 'src/views/component/breadcrumb';

import { mapGetters , mapMutations } from "vuex";

import qs from 'qs';

import videoCreate from './create';
import videoIndex from './index';
import videoEdit from './edit';
import searchForm from './search-form';

 export default {

    name: 'video-views',

    components : {

        vBreadcrumb,

        videoCreate,

        videoIndex,

        videoEdit,

        searchForm
    
    },
    data: function() {	

         return {

            breadcrumbList: [
                {
                    name : '管理员',
                    href : '#',
                },
                {
                    name : '视频专区',
                    href : '#',
                },
                {
                    name: '新建视频',
                    href: '#',
                }
            ],

            query_info : {

                page : 1,

                per_page : 20,

                sort : '',

                order_by : '',

                name : '',

                video_type_id: ''

            },
            
            search_info : {

                page : 1
                
            },                      //分页信息

            table_loading : false,      //表格更新时的loading

            show_modal : false,         //弹框的控制

            bos_config : {},            //百度云上传的配置

            video_detail : {},        //视频详情

            videos : {},     //视频列表

            video_types: {},  // 视频种类

        }
    },
    mounted () {
        this.fetchVideoCategory();
    },
    methods: {

        ...mapMutations('spinShow',[
            'showSpin',
            'closeSpin'
        ]),

        /**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined} 
         */
        changeTab(tab_name) {
            
            if( tab_name === '查看' ) {

                this.fetchVideoList(this.query_info);

            }else {

                this.videos.data = [];

            }

        },

        /**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined} 
         */
        pageChange(pageNumber) {

            this.search_info.page = pageNumber;

            this.search_info.per_page = this.query_info.per_page;

            this.fetchVideoList(this.search_info);
        },

        /**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
        perPageChange(per_page) {

            this.search_info.per_page = per_page;

            this.query_info.per_page = per_page;

            this.fetchVideoList(this.search_info);

        },
        
        /**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
        confirmSearch({ name , video_type_id , sort , order_by }) {

            this.query_info.name = name || '';

            this.query_info.video_type_id = video_type_id || '';

            this.query_info.sort = sort || '';

            this.query_info.order_by = order_by || '';

            this.search_info = JSON.parse(JSON.stringify(this.query_info));

            this.fetchVideoList(this.query_info);

        },

        /**
         * 新增视频的post数据
         * @return {undefined}
         */
        postData(param){

            this.showSpin('请稍后');

            this.$http.post( API_LIST.VIDEO , param )
                    .then(({ data })=>{
                                
                        if( parseInt(data.code) !== 0 )throw new Error(data.error);

                        this.$Message.success('新增成功！');

                        this.$refs.create.handleReset('formValidate');
                                
                    })
                    .catch((err)=>{

                        this.$Message.error(err.message);
                
                    })
                    .then(this.closeSpin)

        },

        /**
         * 查看视频详情
         * @param  {string} id {合作伙伴的id}
         * @return {undefined}
         */
        videoDetail(id){

            this.video_detail = this.videos.data.filter((video) => video.id === id)[0];

            this.show_modal = true;

        },

        /**
         * 更新视频信息
         * @param  {Object} param {需要更细的参数对象}
         * @return {undefined}
         */
        updatevideo(id,param){

            this.showSpin();

            this.$http.post( API_LIST.VIDEO + '/' + id, param )
                .then(({ data })=>{
                            
                    if  ( parseInt(data.code) !== 0 )throw new Error(data.error);

                    this.$Message.success('更新成功！');

                    this.show_modal = false;

                    this.fetchVideoList(this.search_info);
                            
                })
                .catch((err)=>{

                    this.$Message.error(err.message || err.statusText);
            
                })
                .then(this.closeSpin)

        },

        /**
         * 删除视频
         * @param  {string} id {需要删除的视频的id}
         * @return {undefined}
         */
        deletevideo(id){

            this.$Modal.confirm({
                title: '确定删除此视频么？',
                onOk: () => {

                    const param = {
                        _method : 'DELETE'
                    }

                    this.table_loading = true;

                    this.$http.post( API_LIST.VIDEO + '/' + id , param )
                        .then(({ data })=>{
                                    
                            if( parseInt(data.code) !== 0 )throw new Error(data.error);

                            this.fetchVideoList(this.search_info);

                            this.$Message.success('删除成功');
                                    
                        })
                        .catch((err)=>{
                    
                            this.$Message.error(err.message || err.statusText);

                        })
                        .then(() => this.table_loading = false)
                    
                },
                onCancel: () => {}
            });

        },

        /**
         * 获取视频的分类以及bos的配置
         * @return {undefined}
         */
        fetchVideoCategory() {

            this.showSpin('请稍后');

            this.$http.get( API_LIST.VIDEO + '/create')
                    .then(({ data })=>{

                        if( parseInt(data.code) !== 0 )throw new Error(data.error);

                        this.video_types = data.video_types;

                        this.bos_config = data.upload_file_config;

                    })
                    .catch((err)=>{

                        this.$Message.error(err.message || err.statusText);

                    })
                    .then(this.closeSpin)
        },

        /**
         * 获取视频列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
        fetchVideoList(param){

            this.table_loading = true;

            let query_str = qs.stringify(param);

            this.$http.get( API_LIST.VIDEO + '?' + query_str )
                .then(({ data })=>{
                            
                    if( parseInt(data.code) !== 0 )throw new Error(data.error);
                    
                    this.videos = data.videos;
                                         
                })
                .catch((err)=>{
            
                    this.$Message.error( err.message || err.statusText );

                })
                .then(() => this.table_loading = false)

        }
     }
 }
 </script>
